package cz.acies.organizer.data.persist;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.time.DateUtils;

import cz.acies.organizer.data.model.MModelRecord;

public class CalendarTask extends MModelRecord
{
	private static final long serialVersionUID = -3740714450414771942L;
	
	protected String userId;
	protected String subject;
	protected String categories;
	protected String description;
	protected String location;
	protected Timestamp dateStart;
	protected Timestamp dateEnd;
	protected String duration;
	protected boolean allDay;
	
	protected CalendarRecurrence recurrence;
	protected String recRule;

	private java.util.Date emptyDate;
	
	public CalendarTask() {
		this(null, null);
	}

	public CalendarTask(String id) {
		this(id, null);
	}
	
	public CalendarTask(String id, Map<String, Object> data) {
		super(id, data);
		try {
			emptyDate = DateUtils.parseDate("1900-01-01 00:00", "yyyy-MM-dd HH:mm");
		} catch (Exception exc) {
			exc.printStackTrace();
		}
	}

	protected void initialize() {
		/*
		this.modulId = "LX_CALENDAR";
		this.typeProcess = "LX_CALENDAR";
		System.out.println("CalendarTask.initialize(),data: " + data);
		if (this.data != null && !this.data.isEmpty()) {
			if (data.containsKey("recordId")) {
				this.id = (String) data.get("recordId");
			}
			userId = "WX-L-GZ69V88D4ZWFPF8";
			if (data.containsKey("SUBJECT")) {
				this.subject = (String) data.get("SUBJECT");
			}
			if (data.containsKey("LOCATION")) {
				this.location = (String) data.get("LOCATION");
			}
			this.categories = "";
			this.description = "";

			SimpleDateFormat fmt = new SimpleDateFormat("dd.M.yyyy HH:mm");

			if (data.containsKey("DATE_START")) {
				try {
					String s = (String) data.get("DATE_START");
					this.dateStart = new Timestamp(fmt.parse(s).getTime());		
				} catch (Exception exc) {
					exc.printStackTrace();
					this.dateEnd = Timestamp.valueOf("1900-01-01 00:00:00");
				}
			}
			if (data.containsKey("DATE_END")) {
				try {
					String s = (String) data.get("DATE_END");
					this.dateEnd = new Timestamp(fmt.parse(s).getTime());
				} catch (Exception exc) {
					exc.printStackTrace();
					this.dateEnd = Timestamp.valueOf("1900-01-01 00:00:00");
				}
			}
			
			//duration;
			 
			if (data.containsKey("ALL_DAY")) {
				this.allDay = (((String) data.get("ALL_DAY")).equals("1"));
			}
			int x = 0;
		}
		*/
	}
	
	// -----  -----------------------------------------------------------------
	
	public String getUserId() {
		//return (String) getValidObject(userName, "USER_NAME");
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
		//setValidObject(this.userName, userName, "USER_NAME");
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getCategories() {
		return categories;
	}

	public void setCategories(String categories) {
		this.categories = categories;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public Timestamp getDateStart() {
		return dateStart;
	}

	public boolean hasDateStart() {
		try {
			return (this.dateStart != null && DateUtils.isSameInstant(this.dateStart, emptyDate));
		} catch (Exception exc) {
			return false;
		}
	}

	public void setDateStart(Timestamp dateStart) {
		this.dateStart = dateStart;
	}

	public boolean hasDateEnd() {
		try {
			return (this.dateEnd != null && DateUtils.isSameInstant(this.dateEnd, emptyDate));
		} catch (Exception exc) {
			return false;
		}
	}

	public Timestamp getDateEnd() {
		return dateEnd;
	}

	public void setDateEnd(Timestamp dateEnd) {
		this.dateEnd = dateEnd;
	}

	public String getDuration() {
		return duration;
	}

	public void setDuration(String duration) {
		this.duration = duration;
	}

	public boolean isAllDay() {
		return allDay;
	}

	public void setAllDay(boolean allDay) {
		this.allDay = allDay;
	}

	public CalendarRecurrence getRecurrence() {
		return recurrence;
	}

	public void setRecurrence(CalendarRecurrence recurrence) {
		this.recurrence = recurrence;
	}

	public String getRecRule() {
		return recRule;
	}

	public void setRecRule(String recRule) {
		this.recRule = recRule;
	}
}
